Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSpecify: handle Nullability for return types of lambda expressions for Generic Types. #854

Merged

Conversation

akulk022
Copy link
Collaborator

@akulk022 akulk022 commented Oct 29, 2023

class Test {
    interface A<T1 extends @Nullable Object> {
        T1 function(Object o);
    }
    static void testPositive() {
        // BUG: Diagnostic contains: returning @Nullable expression from method with @NonNull return type
        A<String> p = x -> null;
    }
    static void testNegative() {
        // We were reporting a false positive here since the indirect Nullability through the generic was not handled correctly.
        A<@Nullable String> p = x -> null;
    }
}

For the negative scenario mentioned above where we do not expect an exception, we were getting a false positive. After computing the Nullability correctly when it is indirectly applied through the generic type we no longer get this false positive.

All the test cases in NullAwayJSpecifyGenericTests.java have passed for these changes.

Similar changes for handling lambda parameters were made under #852

@codecov
Copy link

codecov bot commented Oct 29, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (def015a) 86.92% compared to head (b88266a) 86.93%.

Additional details and impacted files
@@             Coverage Diff              @@
##             master     #854      +/-   ##
============================================
+ Coverage     86.92%   86.93%   +0.01%     
- Complexity     1883     1886       +3     
============================================
  Files            74       74              
  Lines          6210     6215       +5     
  Branches       1206     1208       +2     
============================================
+ Hits           5398     5403       +5     
  Misses          405      405              
  Partials        407      407              
Files Coverage Δ
...away/src/main/java/com/uber/nullaway/NullAway.java 89.83% <100.00%> (+0.04%) ⬆️

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@msridhar msridhar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good start!

Comment on lines 876 to 882
} else if (config.isJSpecifyMode()
&& GenericsChecks.getGenericMethodReturnTypeNullness(
methodSymbol, ASTHelpers.getType(tree), state, config)
.equals(Nullness.NULLABLE)) {
// Get the Nullness if the Annotation is indirectly applied through a generic type if we
// are in JSpecify mode
return Description.NO_MATCH;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code won't work for the case when the lambda is written x -> { return null; }. I updated the test and we get a false positive. The issue is that with the return null case, the tree passed in is the ReturnTree, not the tree for the lambda itself. One way to fix this would be to add another parameter @Nullable Tree lambdaTree to checkReturnExpression and then only run this case when lambdaTree is non-null. The extant tree parameter can be renamed to errorTree, since its main purpose is for finding the right locations to report errors.

@msridhar msridhar marked this pull request as ready for review November 1, 2023 15:59
Copy link
Collaborator

@msridhar msridhar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@msridhar msridhar merged commit 81d3cfd into uber:master Nov 1, 2023
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants